﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using Abs.Core.Models.Blog;
using Abs.Core.DAL.EntityFramework;
using Abs.Core.Models.User;

namespace Abs.App.Controllers
{ 
    public class PostController : Controller
    {
        private readonly AbsContext db = new AbsContext();

        //
        // GET: /Post/

        public ActionResult Index()
        {
            if (this.Session["Email"] == null)
                return this.Redirect("~/User/Unauthorized_Access.aspx");
            

            var posts = new List<Post>();

            if ((int)this.Session["Role"] == (int)RoleEnum.User)
            {
                posts = db.Posts
                    .Include(p => p.User)
                    .Include(p => p.Category)
                    .Include(p => p.Status)
                    .Where(p => p.StatusId == (int) StatusEnum.Approved)
                    .Select(p => p).ToList();
            }//end of User Role
            else
            {
                posts = db.Posts
                    .Include(p => p.User)
                    .Include(p => p.Category)
                    .Include(p => p.Status)
                    .Select(p => p).ToList();
            }//end of Admin Role

            return View(posts);
        }

        //
        // GET: /Post/Details/5

        public ActionResult Details(int id)
        {
            if (this.Session["Email"] == null)
                return this.Redirect("~/User/Unauthorized_Access.aspx");

            Post post = db.Posts.Find(id);

            if (post == null)
                return RedirectToAction("Index");

            return View(post);
        }

        //
        // GET: /Post/Create

        public ActionResult Create()
        {
            ViewBag.UserEmail = new SelectList(db.Users, "Email", "Name");
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Title");
            ViewBag.StatusId = new SelectList(db.Statuses, "StatusId", "Title");
            return View();
        } 

        //
        // POST: /Post/Create

        [HttpPost]
        public ActionResult Create(Post post)
        {
            if (this.Session["Email"] == null)
                return this.Redirect("~/User/Unauthorized_Access.aspx");

            post.UserEmail = (string) this.Session["Email"];
            post.Date = DateTime.Now;

            if (ModelState.IsValid)
            {
                db.Posts.Add(post);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.UserEmail = new SelectList(db.Users, "Email", "Name", post.UserEmail);
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Title", post.CategoryId);
            ViewBag.StatusId = new SelectList(db.Statuses, "StatusId", "Title", post.StatusId);
            return View(post);
        }
        
        //
        // GET: /Post/Edit/5
 
        public ActionResult Edit(int id)
        {
            if (this.Session["Email"] == null)
                return this.Redirect("~/User/Unauthorized_Access.aspx");

            Post post = db.Posts.Find(id);

            if (post == null)
                return RedirectToAction("Index");

            if((string) this.Session["Email"] != post.UserEmail && 
                (int) this.Session["Role"] != (int) RoleEnum.Admin)
                return this.Redirect("~/User/Unauthorized_Access.aspx");

            ViewBag.UserEmail = new SelectList(db.Users, "Email", "Name", post.UserEmail);
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Title", post.CategoryId);
            ViewBag.StatusId = new SelectList(db.Statuses, "StatusId", "Title", post.StatusId);
            return View(post);
        }

        //
        // POST: /Post/Edit/5

        [HttpPost]
        public ActionResult Edit(Post post)
        {
            if (this.Session["Email"] == null)
                return this.Redirect("~/User/Unauthorized_Access.aspx");

            if ((string)this.Session["Email"] != post.UserEmail &&
                (int)this.Session["Role"] != (int)RoleEnum.Admin)
                return this.Redirect("~/User/Unauthorized_Access.aspx");

            if (ModelState.IsValid)
            {
                db.Entry(post).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.UserEmail = new SelectList(db.Users, "Email", "Name", post.UserEmail);
            ViewBag.CategoryId = new SelectList(db.Categories, "CategoryId", "Title", post.CategoryId);
            ViewBag.StatusId = new SelectList(db.Statuses, "StatusId", "Title", post.StatusId);
            return View(post);
        }

        //
        // GET: /Post/Delete/5
 
        public ActionResult Delete(int id)
        {
            if (this.Session["Email"] == null)
                return this.Redirect("~/User/Unauthorized_Access.aspx");
            if ((int)this.Session["Role"] != (int)RoleEnum.Admin)
                return this.Redirect("~/User/Unauthorized_Access.aspx");

            Post post = db.Posts.Find(id);

            if (post == null)
                return RedirectToAction("Index");

            return View(post);
        }

        //
        // POST: /Post/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            if (this.Session["Email"] == null)
                return this.Redirect("~/User/Unauthorized_Access.aspx");
            if((int)this.Session["Role"] != (int)RoleEnum.Admin)
                return this.Redirect("~/User/Unauthorized_Access.aspx");

            Post post = db.Posts.Find(id);

            if (post == null)
                return RedirectToAction("Index");

            db.Posts.Remove(post);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}